package cn.school.demo

import android.app.PendingIntent
import android.content.Context
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.navigation.NavDeepLinkBuilder
import cn.school.demo.pojo.Notify
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.reflect.TypeToken
import com.tencent.android.tpush.*

class MessageReceiver : XGPushBaseReceiver() {

    override fun onRegisterResult(p0: Context?, p1: Int, p2: XGPushRegisterResult?) {

    }

    override fun onUnregisterResult(p0: Context?, p1: Int) {

    }

    override fun onSetTagResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onDeleteTagResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onSetAccountResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onDeleteAccountResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onSetAttributeResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onQueryTagsResult(p0: Context?, p1: Int, p2: String?, p3: String?) {

    }

    override fun onDeleteAttributeResult(p0: Context?, p1: Int, p2: String?) {

    }

    override fun onTextMessage(p0: Context, p1: XGPushTextMessage?) {
        /*val n = NotificationUtils(p0,p1?.pushChannel.toString(),"学弈有道默认通道")
        n.sendNotification(1,"这是标题","This is context!",R.drawable.user_my_icon)*/
    }

    override fun onNotificationClickedResult(
        context: Context?,
        message: XGPushClickedResult?
    ) {
        LogUtils.d("此方法获取抵达的通知点击事件！")
        if (context == null || message == null) {
            return
        }
        var text = ""
        val pendingIntent: PendingIntent
        LogUtils.d(message.customContent)
        try {
            val map = GsonUtils.fromJson<Map<String, String>>(
                message.customContent,
                object : TypeToken<Map<String, String>>() {}.type
            )
            if (map["文章"] != null) {
                val id = map["文章"].toString().toInt()
                LogUtils.e("=====================${id}=======================================")
                pendingIntent = NavDeepLinkBuilder(context)
                    .setGraph(R.navigation.mobile_navigation)
                    .setDestination(R.id.ArticleEntity)
                    .setArguments(null)
                    .createPendingIntent()
                MyApplication.apply {
                    isNotify = true
                    articleId = id
                    notifyList[message.content] = Notify(1, pendingIntent, id, null, null, null)
                }
            } else if (map["答卷"] != null) {
                val _map = mapOf("title" to message.content, "introduction" to "来测一测你自己吧！")
                val bundle: Bundle = bundleOf(Pair("map", GsonUtils.toJson(_map)))
                val id = map["答卷"].toString().toInt()
                LogUtils.e("=====================${id}=======================================")
                val startTime = map["startTime"].toString()
                val endTime = map["endTime"].toString()
                pendingIntent = NavDeepLinkBuilder(context)
                    .setGraph(R.navigation.mobile_navigation)
                    .setDestination(R.id.Answer)
                    .setArguments(bundle)
                    .createPendingIntent()
                MyApplication.apply {
                    isSubject = true
                    signId = id
                    isNotify = true
                    answerTopic = message.content
                    notifyList[message.content] =
                        Notify(2, pendingIntent, null, id, startTime, endTime)
                }
            } else if (map["通知"] != null) {
                val id = map["通知"].toString().toInt()
                LogUtils.e("=====================${id}=======================================")
                pendingIntent = NavDeepLinkBuilder(context)
                    .setGraph(R.navigation.mobile_navigation)
                    .setDestination(R.id.ArticleEntity)
                    .setArguments(null)
                    .createPendingIntent()
                MyApplication.apply {
                    isNotify = true
                    articleId = id
                    notifyList[message.content] = Notify(3, pendingIntent, id, null, null, null)
                }
            } else {
                LogUtils.e("=====================我出错了AAAAA=======================================")
                pendingIntent = NavDeepLinkBuilder(context)
                    .setGraph(R.navigation.mobile_navigation)
                    .setDestination(R.id.Article)
                    .setArguments(null)
                    .createPendingIntent()
            }
            if (message.actionType == NotificationAction.clicked.type.toLong()) {
                // 通知在通知栏被点击
                // APP自己处理点击的相关动作
                LogUtils.e(MyApplication.articleId, MyApplication.signId)
                text = "通知被打开 : $message"
                if (MyApplication.user != null) {
                    pendingIntent.send()
                } else {
                    ToastUtils.showShort("请先登录！")
                    MyApplication.pendingIntent = pendingIntent
                }
            } else if (message.actionType == NotificationAction.delete.type.toLong()) {
                // 通知被清除
                // APP自己处理通知被清除后的相关动作
                text = "通知被清除 : ${message}"
            }
        } catch (e: Error) {
            e.printStackTrace()
        }
        // APP自主处理的过程。
        LogUtils.e(text, "广播接收到通知:$text", message.title, message.content, message.customContent)
    }

    override fun onNotificationShowedResult(p0: Context, p1: XGPushShowedResult?) {
        LogUtils.d("此方法获取抵达的通知内容！,${p1?.pushChannel}")

    }

}